Page({
  async saveQRCode() {
    const { path } = await wx.getImageInfo({ src: this.data.url })

    try {
      await wx.saveImageToPhotosAlbum({ filePath: path })
      wx.utils.toast('保存成功')
    } catch (err) {
      wx.utils.toast('保存失败，请重试！')
    }
  },

  async onLoad({ id, encryptedData }) {
    let res
    // 自己进来的
    if (id) {
      res = await wx.http({
        url: `/visitor/${id}`,
      })
    }
    // 分享给朋友进来的
    else if (encryptedData) {
      res = await wx.http({
        url: `/visitor/share/${encryptedData}`,
      })
    }

    this.setData(res.data)
  },

  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
})
